﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.General.MVP;
using ESF.SSO.Core.Entities;
using ESF.SSO.Core.BusinessLogic;
using ESF.SSO.Core.Entities.BasicReferences;
using System.Data;
using ESF.General.Entity;

#endregion


namespace ESF.SSO.Presenter.Admin.User
{
	public class BrowseContactUsPresenter : BaseBrowsePagedPresenter<ContactUs, IBrowseContactUsViewer>
	{
		#region Constructor
		
		public BrowseContactUsPresenter (IBrowseContactUsViewer pViewer) : base (pViewer) {}
		
		#endregion
		
		
		#region Funciones

		public override void Load()
		{
			LoadSteps ();
		}


		public void LoadSteps()
		{
			_Viewer.ExtraAppSteps = CtrlBasicReference.List(BasicReferenceTypes.Application).Where(a => !string.IsNullOrEmpty(a.Extra_II)).ToList ();
		}


		public override void Clear()
		{
			base.Clear();

			_Viewer.filMessageEndDate = null;
			_Viewer.filMessageStartDate = null;
			_Viewer.filUserFullName = string.Empty;
			_Viewer.filUserIdentificationTypeID = null;
			_Viewer.filUserIdentificationNumber = string.Empty;
			_Viewer.filSenderEmail = string.Empty;
			_Viewer.filContactUsTypeID = null;
			_Viewer.filWithResponse = null;

			_Viewer.CountResumen = string.Empty;

			_Viewer.filExtraValue = string.Empty;
		}


		protected override General.Entity.BrowseResults<ContactUs> GetPagedResults(General.Entity.PaggingInfo pPaggingInfo, out ContactUs pFilter)
		{
			int readyCount, pendingCount;

			BrowseResults<ContactUs> result;


			pFilter = new ContactUs () 
			{ 
				filUserAlias = _Viewer.filUserFullName, 
				filUserFullName = _Viewer.filUserFullName,
				filUserIdentificationNumber = _Viewer.filUserIdentificationNumber,
				filUserIdentificationTypeID = _Viewer.filUserIdentificationTypeID,
				filMessageStartDate = _Viewer.filMessageStartDate, 
				filMessageEndDate = _Viewer.filMessageEndDate,
				filWithResponse = _Viewer.filWithResponse,
				SenderEmail = _Viewer.filSenderEmail,
				filExtraValue = _Viewer.filExtraValue,
				ContactUsTypeID = new General.Entity.BasicReference () { ID = _Viewer.filContactUsTypeID } 
			};

			result = CtrlContactUs.Browse (
				pFilter,
				pPaggingInfo,
				out readyCount,
				out pendingCount);

			_Viewer.CountResumen = string.Format ("Atendidos: {0} / Pendientes: {1}", readyCount, pendingCount);


			return result;
		}


		public void SendResponse ()
		{
			try
			{
				CtrlContactUs.InsertResponse (
					new ContactUs () { 
						ID = _Viewer.SelectedID, 
						ResponseUserAlias = _Viewer.frmUserAlias,
						CreationDateTime = _Viewer.SelectedCreationDateTime,
						Title = _Viewer.SelectedTitle,
						Response = _Viewer.SelectedResponse,
						SenderEmail = _Viewer.SelectedSenderEmail });

				Browse ();

				_Viewer.frmInfoMsg = "La Respuesta ha sido registrada y enviada";
			}
			catch (Exception pEx)
			{
				LogError (pEx);
			}
		}

		#endregion
	}
}
